#include "cms_common.h"
#include "cms_log.h"
#include "core_utils.h"

extern cms_moduleList_t *moduleList;
extern cms_eventList_t  *eventList;

void cmsCore_printModuleList(void){
	cms_moduleList_t *pSeekModule = NULL;
	cms_hookEventList_t *pSeekHookEvent = NULL;
	
	if(DEBUG_LOG){
		pSeekModule = moduleList;
		while(pSeekModule != NULL){
			CMS_LOG(LL_INFO, "moduleName:%s, mid:%d, eid:%d, pid: %d.\n",
				pSeekModule->module->moduleInfo.moduleName,
				pSeekModule->module->moduleInfo.moduleMsqId,
				pSeekModule->module->moduleInfo.eventMsqId, 
				pSeekModule->module->moduleInfo.pid);

			pSeekHookEvent = pSeekModule->module->eventListOfModule;
			while(pSeekHookEvent != NULL){
				CMS_LOG(LL_INFO, "eventId %d.\n", pSeekHookEvent->eventID);
				pSeekHookEvent = pSeekHookEvent->next;
			}
			
			pSeekModule = pSeekModule->next;
		}
	}
}

void cmsCore_printEventList(void){
	cms_eventList_t *pSeekEvent = NULL;
	cms_hookModuleList_t *pSeekHookModule = NULL;

	if(DEBUG_LOG){
		pSeekEvent = eventList;

		while(pSeekEvent != NULL){
			CMS_LOG(LL_INFO, "eventId %d.\n", pSeekEvent->event->eventID);

			pSeekHookModule = pSeekEvent->event->moduleListOfEvent;
			while(pSeekHookModule != NULL){
				CMS_LOG(LL_INFO, "moduleName:%s, mid:%d, eid:%d, pid %d.\n",
					pSeekHookModule->moduleInfo.moduleName,
					pSeekHookModule->moduleInfo.moduleMsqId,
					pSeekHookModule->moduleInfo.eventMsqId,
					pSeekHookModule->moduleInfo.pid);
				
				pSeekHookModule = pSeekHookModule->next;
			}

			pSeekEvent = pSeekEvent->next;
		}
	}
}

cms_moduleList_t *cmsCore_seekModuleByID(uint32_t moduleId){
	cms_moduleList_t *pSeekModule = moduleList;

	/*find module in list,check if has exist*/
	while((pSeekModule != NULL )&& (pSeekModule->module->moduleInfo.moduleMsqId != moduleId)){
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}

	return pSeekModule;
}

cms_moduleList_t *cmsCore_seekModuleByName(char *moduleName){
	cms_moduleList_t *pSeekModule = moduleList;

	/*find module in list,check if has exist*/
	while((pSeekModule != NULL )&& strcmp(pSeekModule->module->moduleInfo.moduleName, moduleName)){
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}

	return pSeekModule;
}

cms_eventList_t *seekEvent(uint32_t eventId){
	cms_eventList_t *pSeekEvent = eventList;

	while((pSeekEvent != NULL )&& (pSeekEvent->event->eventID != eventId)){
		if(pSeekEvent->next == NULL){
			pSeekEvent = NULL;
			break;
		}
		pSeekEvent = pSeekEvent->next;
	}

	return pSeekEvent;
}

int	sendEvent(uint32_t eventMsqId, uint32_t pid, cmsEvent_t *eventBuf, uint32_t eventLen){
	CMS_LOG(LL_DEBUG, "before send event to dst module, value %s len %d======================\n",
		eventBuf->notify.eventNotify.eventContent, eventLen);
	
	//union sigval sigValue;
	//sigValue.sival_int = eventMsqId;
	
	if((msgsnd(eventMsqId, eventBuf, eventLen, IPC_NOWAIT)) == -1){
		CMS_LOG(LL_ERROR, "send msg fail.\n");
		return -1;
	}
	
	//if((sigqueue(pid, SIGEVENT, sigValue)) == -1){
	//	CMS_LOG(LL_ERROR, "sigqueue send fail.\n");
	//	return -1;
	//}
	
	return 0;
}

cms_moduleList_t *seekModule(char *moduleName){
	cms_moduleList_t *pSeekModule = moduleList;

	while(pSeekModule && strcmp(pSeekModule->module->moduleInfo.moduleName, moduleName)){
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}

	return pSeekModule;
}

int cmsCore_forwardMsg(cmsMsgCb_t *pMsg, uint32_t dtsId){
	int ret = 0;

	ret = cms_sendMsg(pMsg, dtsId);
	cms_msgFree(pMsg);

	return ret;
}

int	cmsCore_notify(cmsMsgCb_t *pMsg){
	cmsMsgCb_t *pCurMsg = NULL;
	cmsMsg_action_t *pAction = NULL;
	cms_eventList_t *pSeekEvent = NULL;
	cms_hookModuleList_t *pCurHookModule = NULL;
	cms_moduleList_t *pCurModule = NULL;
	cmsEvent_t *eventBuf = NULL;
	uint32_t eventLen = 0;
	uint32_t actionNum = 0;
	//union sigval sigValue;

	pCurMsg = pMsg;
	actionNum = pCurMsg->header.actionNum;
	pSeekEvent = eventList;

	//cmsCore_printEventList();
	//cmsCore_printModuleList();
	while(actionNum != 0){	
		pAction = cms_parseMsgAction(pCurMsg);		
		if(pAction->action->type == CMS_MSG_ACT_EVENT_NOTIFY){
			CMS_LOG(LL_INFO, "eventID: %d.\n", pAction->event->eventId[0]);
			pSeekEvent = seekEvent(pAction->event->eventId[0]);
			if(pSeekEvent){
				#if 1
				/*alloc memory and assign value according different event types*/
				if(pAction->eventContent->typeSizeOfValue == PARAMETER_TYPE_STRING){//event type is string 
					eventLen = strlen((char *)(pAction->eventContent->value)) + 1;
					eventBuf = (cmsEvent_t *)calloc(1, sizeof(cmsEvent_t)+eventLen);
					
					memcpy(eventBuf->notify.eventNotify.eventContent, pAction->eventContent->value, strlen((char *)(pAction->eventContent->value))+1);
				}else{// other event type
					eventLen = pAction->eventContent->typeSizeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS);
					eventBuf = (cmsEvent_t *)calloc(1, sizeof(cmsEvent_t)+eventLen);
	
					memcpy(eventBuf->notify.eventNotify.eventContent, pAction->eventContent->value, eventLen);
				}
				eventBuf->notifyType |= CMS_MSG_ACT_EVENT_NOTIFY;
				eventBuf->notify.eventNotify.eventId = pAction->event->eventId[0];
				eventBuf->notify.eventNotify.typeSizeOfEContent = pAction->eventContent->typeSizeOfValue;
				#else
				eventLen = strlen((char *)(pAction->eventContent->value));
				if((eventLen > EVENT_CONTENT_MAX) || ((pAction->eventContent->typeSizeOfValue &~((uint32_t)PARAMETER_TYPE_CLASS_BITS)) > EVENT_CONTENT_MAX)){
					CMS_LOG(LL_ERROR, "cms event content is exceed the limit(%d)!!!\n", EVENT_CONTENT_MAX);
					return -1;
				}

				memset(&eventBuf, 0, sizeof(cmsEvent_t));
				
				if(eventLen == 0)
					eventBuf.notify.eventNotify.eventContent[0] = '\0';
				else{
					if(pAction->eventContent->typeSizeOfValue == PARAMETER_TYPE_STRING)
						memcpy(eventBuf.notify.eventNotify.eventContent, pAction->eventContent->value, eventLen);
					else
						memcpy(eventBuf.notify.eventNotify.eventContent, pAction->eventContent->value, pAction->eventContent->typeSizeOfValue&~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
				}
				eventBuf.notifyType |= CMS_MSG_ACT_EVENT_NOTIFY;
				eventBuf.notify.eventNotify.eventId = pAction->event->eventId[0];
				eventBuf.notify.eventNotify.typeSizeOfEContent = pAction->eventContent->typeSizeOfValue;
				#endif
				CMS_LOG(LL_INFO, "cmsCore--received event notify, value %s len %d======================\n", eventBuf->notify.eventNotify.eventContent, eventLen);

				/*send event buf to cms_task*/
				pCurHookModule = pSeekEvent->event->moduleListOfEvent;
				while(pCurHookModule != NULL){
					
					//sigValue.sival_int = pCurHookModule->moduleInfo.eventMsqId;

					CMS_LOG(LL_INFO,"before send out event %d, %s %s, msqid %d, pid%d=============================\n", 
						eventBuf->notify.eventNotify.eventId, pCurHookModule->moduleInfo.moduleName, CMS_TASK, pCurHookModule->moduleInfo.eventMsqId,pCurHookModule->moduleInfo.pid);
					if((msgsnd(pCurHookModule->moduleInfo.eventMsqId, eventBuf, sizeof(cmsEvent_t)+eventLen-sizeof(long), IPC_NOWAIT)) == -1){
						CMS_LOG(LL_ERROR, "send event msg to %s fail.\n", pCurHookModule->moduleInfo.moduleName);
						free(eventBuf);
						eventBuf = NULL;
						return -1;
					}
					/*do not sigqueue to cms_task*/
					#if 0
					if(strcmp(pCurHookModule->moduleInfo.moduleName, CMS_TASK)){
						if((sigqueue(pCurHookModule->moduleInfo.pid, SIGEVENT, sigValue)) == -1){
							CMS_LOG(LL_ERROR, "sigqueue send(%s) fail.\n", pCurHookModule->moduleInfo.moduleName);
							//free(eventBuf);
							//return -1;
						}
					}
					//else{
						//if((kill(pCurHookModule->moduleInfo.pid, SIGEVENT)) == -1){
						//	CMS_LOG(LL_ERROR, "kill send fail.\n");
						//	free(eventBuf);
						//	return -1;
						//}
					//}
					#endif
					pCurHookModule = pCurHookModule->next;
				}

			}else{
				CMS_LOG(LL_ERROR, "not found eventID: %d.\n", pAction->event->eventId[0]);
				free(eventBuf);
				eventBuf = NULL;
				return -1;
			}
			
		}
		else if(pAction->action->type == CMS_MSG_ACT_OBJCHG_NOTIFY){
			CMS_LOG(LL_INFO, "objchg notify %d.\n", pAction->objDataShm->objChgShmId);
			eventBuf = (cmsEvent_t *)calloc(1, sizeof(cmsEvent_t));
			eventBuf->notifyType |= CMS_MSG_ACT_OBJCHG_NOTIFY;
			eventBuf->notify.objNotify.objChgShmId = pAction->objDataShm->objChgShmId;

			/*send event buf to cms_task*/
			pCurModule = seekModule(CMS_TASK);
			if(pCurModule){
				sendEvent(pCurModule->module->moduleInfo.eventMsqId, pCurModule->module->moduleInfo.pid, eventBuf, sizeof(cmsEvent_t)-sizeof(long));
			}
		}
		free(eventBuf);
		eventBuf = NULL;
		
		actionNum--;
	}
	
	return 0;
}

int cmsCore_regModule(const cmsMsg_action_t *pAction){
	cms_module_t *pModule = NULL;
	cms_moduleList_t *pInsertModuleList = NULL;
	cms_moduleList_t *pSeekModule = NULL;
	cms_eventList_t *pSeekEvent = NULL;
	cms_hookModuleList_t *pCurHookModule = NULL;
	char *moduleName = NULL;
	uint32_t moduleMsqId, eventMsqId, pid;
	cmsMsqBuf_t msqBuf;

	if(pAction == NULL){
		CMS_LOG(LL_ERROR, "regModule fail, parameter is NULL.\n");
		return -1;
	}
	
	moduleName = pAction->module->moduleName;
	moduleMsqId = pAction->module->moduleMsqId;
	eventMsqId = pAction->module->eventMsqId;
	pid = pAction->module->pid;
		
	pSeekModule = moduleList;
	if(pSeekModule == NULL){
		CMS_LOG(LL_ERROR, "cms core module list is NULL.\n");
		return -1;
	}

	/*find module in list,check if has exist*/
	while(strcmp(pSeekModule->module->moduleInfo.moduleName, moduleName)){
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}
	
	if(pSeekModule != NULL){//module has exist, update the parameter of module
		if(moduleMsqId != pSeekModule->module->moduleInfo.moduleMsqId){
			while(-1 != msgrcv(pSeekModule->module->moduleInfo.moduleMsqId, &msqBuf, sizeof(msqBuf)-sizeof(long),0, IPC_NOWAIT)){
				/* clean msg in old moduleMsq */
				while(msqBuf.shmNum > 0){
					shmctl(msqBuf.shmId[msqBuf.shmNum-1], IPC_RMID, NULL);
					msqBuf.shmNum--;
				}
			}
			msgctl(pSeekModule->module->moduleInfo.moduleMsqId, IPC_RMID, NULL);
		}
		if(eventMsqId != pSeekModule->module->moduleInfo.eventMsqId)
			msgctl(pSeekModule->module->moduleInfo.eventMsqId, IPC_RMID, NULL);
		pSeekModule->module->moduleInfo.moduleMsqId = moduleMsqId;
		pSeekModule->module->moduleInfo.eventMsqId = eventMsqId;
		pSeekModule->module->moduleInfo.pid = pid;

		//update event list info
		pSeekEvent = eventList;
		while(pSeekEvent){
			pCurHookModule = pSeekEvent->event->moduleListOfEvent;
			while(pCurHookModule){
				if(!strcmp(pCurHookModule->moduleInfo.moduleName, moduleName)){
					pCurHookModule->moduleInfo.eventMsqId = eventMsqId;
					pCurHookModule->moduleInfo.moduleMsqId = moduleMsqId;
					pCurHookModule->moduleInfo.pid = pid;
				}
				pCurHookModule = pCurHookModule->next;
			}
	
			pSeekEvent = pSeekEvent->next;
		}
		//strcpy(pModule->moduleInfo.moduleName, moduleName);
	}else{//not exist, insert to list
		pModule = (cms_module_t *)malloc(sizeof(cms_module_t));
		if(pModule != NULL){
			pModule->moduleInfo.moduleMsqId = moduleMsqId;
			pModule->moduleInfo.eventMsqId = eventMsqId; 
			pModule->moduleInfo.pid = pid;
			pModule->eventListOfModule = NULL;
			strcpy(pModule->moduleInfo.moduleName, moduleName);
		}else{
			CMS_LOG(LL_ERROR, "reg module fail, malloc pModule fail.\n");
			return -1;
		}

		pInsertModuleList = (cms_moduleList_t *)malloc(sizeof(cms_moduleList_t));
		if(pInsertModuleList != NULL){
			pInsertModuleList->module = pModule;
			INSERT_NODE(moduleList,pInsertModuleList);
		}else{
			CMS_LOG(LL_ERROR, "reg module fail, malloc pInsertModuleList fail.\n");
			safe_free(pModule);
			return -1;
		}
	}

	if(DEBUG_LOG){
		pSeekModule = moduleList;
		while(pSeekModule != NULL){
			CMS_LOG(LL_INFO, "current modlelist elemeter: name-%s, mid-%d, eid-%d, pid-%d.\n", pSeekModule->module->moduleInfo.moduleName,
				pSeekModule->module->moduleInfo.moduleMsqId,pSeekModule->module->moduleInfo.eventMsqId, pSeekModule->module->moduleInfo.pid);
			pSeekModule = pSeekModule->next;
		}
	}

	return 0;
}

int cmsCore_unregModule(const cmsMsg_action_t *pAction){
	cms_moduleList_t *pSeekModule = NULL;
	cms_hookEventList_t *pCurEvent = NULL, *pTmpEvent = NULL;
	cms_eventList_t *pSeekEvent = NULL;
	cms_hookModuleList_t *pCurModule = NULL, *pTmpModule = NULL;
	char *moduleName = NULL;
	
	if(pAction == NULL){
		CMS_LOG(LL_ERROR, "unregModule fail, action is NULL.\n");
		return -1;
	}
	pSeekModule = moduleList;
	if(pSeekModule == NULL){
		CMS_LOG(LL_ERROR, "cms core module list is NULL.\n");
		return -1;
	}

	moduleName = pAction->module->moduleName;
	CMS_LOG(LL_DEBUG, "pAction to unreg module:%s pid:%d.\n", pAction->module->moduleName, pAction->module->pid);

	/*find module in list,check if has exist*/
	while(strcmp(pSeekModule->module->moduleInfo.moduleName, moduleName) != 0){
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}

	/*free module and its event*/
	if(pSeekModule != NULL){
		CMS_LOG(LL_DEBUG,"find pSeekModule to unreg: module:%s pid:%d.", pSeekModule->module->moduleInfo.moduleName, pSeekModule->module->moduleInfo.pid);
		if(pSeekModule->module->eventListOfModule != NULL){
			pCurEvent = pSeekModule->module->eventListOfModule;
			while(pCurEvent != NULL){
				pTmpEvent = pCurEvent;
				pCurEvent = pCurEvent->next;
				DELETE_NODE(pSeekModule->module->eventListOfModule, pTmpEvent);
			}
		}
		DELETE_NODE(moduleList, pSeekModule);
	}else{
		CMS_LOG(LL_INFO, "cms_core have not find the unreg module.\n");
	}

	/*find module in evet hookModule list*/
	pSeekEvent = eventList;
	while(pSeekEvent != NULL){
		pCurModule = pSeekEvent->event->moduleListOfEvent;
		while(pCurModule != NULL){
			if(strcmp(pCurModule->moduleInfo.moduleName, moduleName) == 0){
				pTmpModule = pCurModule;
				pCurModule = pCurModule->next;

				DELETE_NODE(pSeekEvent->event->moduleListOfEvent, pTmpModule);

				break;
			}
			pCurModule = pCurModule->next;
		}

		pSeekEvent = pSeekEvent->next;
	}

	if(DEBUG_LOG){
		pSeekModule = moduleList;
		while(pSeekModule != NULL){
			CMS_LOG(LL_INFO, "current modlelist elemeter: name-%s, mid-%d, eid-%d.\n",pSeekModule->module->moduleInfo.moduleName,
				pSeekModule->module->moduleInfo.moduleMsqId,pSeekModule->module->moduleInfo.eventMsqId);
			pSeekModule = pSeekModule->next;
		}
	}
	
	return 0;
}


int cmsCore_subscribeEvent(event_t *event, int moduleMsqId){
	 cms_eventList_t *pNewEventList = NULL, *pCurEventList = NULL;
	 cms_moduleList_t *pSeekModule = moduleList;
	 cms_event_t *pNewEvent = NULL;
	 cms_hookModuleList_t *pNewModuleList = NULL, *pCurModuleList = NULL;
	 cms_hookEventList_t *pCurHookEvent = NULL, *pNewHookEvent = NULL;
	 int i = 0;
	 int isFound = 0;

	 /*find module by moduleMsqId*/
	while(pSeekModule != NULL){
	 	if(pSeekModule->module->moduleInfo.moduleMsqId == moduleMsqId){
			break;
		}
		if(pSeekModule->next == NULL){
			pSeekModule = NULL;
			break;
		}
		pSeekModule = pSeekModule->next;
	}
	 

	/*insert event to hookevent of moduleList*/
	if(pSeekModule != NULL){
		pCurHookEvent = pSeekModule->module->eventListOfModule;

		for(i = 0; i < event->eventCount; i++){
		 	isFound = 0;
			while(pCurHookEvent != NULL){
				if(event->eventId[i] == pCurHookEvent->eventID){
					isFound = 1;
					break;
				}
				pCurHookEvent = pCurHookEvent->next;
			}
			if(isFound == 0){
				pNewHookEvent = (cms_hookEventList_t *)calloc(1, sizeof(cms_hookEventList_t));
				if(pNewHookEvent != NULL){
					pNewHookEvent->eventID = event->eventId[i];
					pNewHookEvent->next = NULL;
					pNewHookEvent->prev = NULL;
					INSERT_NODE(pSeekModule->module->eventListOfModule,pNewHookEvent);
				}
			}
	 	}
	}else{
		CMS_LOG(LL_ERROR, "subscribe event failed, because find module by moduleMsqId fail.\n");
		return -1;
	}


	 /*insert module to hookModule of event list*/
	 for(i = 0; i < event->eventCount; i++){
	 	pCurEventList = seekEvent(event->eventId[i]);
		if(pCurEventList == NULL){
			pNewEvent = (cms_event_t *)calloc(1, sizeof(cms_event_t));
			if(pNewEvent != NULL){
				pNewModuleList = (cms_hookModuleList_t *)calloc(1, sizeof(cms_hookModuleList_t));
				pNewModuleList->moduleInfo = pSeekModule->module->moduleInfo;
				pNewEvent->eventID = event->eventId[i];
				pNewEvent->moduleListOfEvent = pNewModuleList;

				pNewEventList = (cms_eventList_t *)calloc(1, sizeof(cms_eventList_t));
				pNewEventList->event = pNewEvent;
				pNewEventList->next = NULL;
				pNewEventList->prev = NULL;

				INSERT_NODE(eventList,pNewEventList);
			}
		}else{
			isFound = 0;
			pCurModuleList = pCurEventList->event->moduleListOfEvent;
			while(pCurModuleList != NULL){
				if(pCurModuleList->moduleInfo.moduleMsqId == moduleMsqId){
					isFound = 1;
					break;
				}
				pCurModuleList = pCurModuleList->next;
			}
			if(isFound == 0){
				pNewModuleList = (cms_hookModuleList_t *)calloc(1, sizeof(cms_hookModuleList_t));
				pNewModuleList->moduleInfo = pSeekModule->module->moduleInfo;
				INSERT_NODE(pCurEventList->event->moduleListOfEvent,pNewModuleList);
			}
		}
	 }

	 return 0;
}

int cmsCore_unSubscribeEvent(event_t *event, int moduleMsqId){
	cms_moduleList_t *pSeekModule = NULL;
	cms_hookEventList_t *pSeekHookEvent = NULL, *pTmpHookEvent = NULL;
	cms_eventList_t *pSeekEvent = NULL;
	cms_hookModuleList_t *pSeekHookModule = NULL, *pTmpModule = NULL;
	int i = 0;
	int isFound = 0;

	/*remove event from hookevent of moduleList*/
	pSeekModule = moduleList;
	while(pSeekModule != NULL){
		CMS_LOG(LL_INFO, "moduleName:%s, mid:%d, eid:%d.\n", pSeekModule->module->moduleInfo.moduleName,
			pSeekModule->module->moduleInfo.moduleMsqId,pSeekModule->module->moduleInfo.eventMsqId);

		if(pSeekModule->module->moduleInfo.moduleMsqId == moduleMsqId){
			pSeekHookEvent = pSeekModule->module->eventListOfModule;
			while(pSeekHookEvent != NULL){
				isFound = 0;
				//CMS_LOG(LL_DEBUG, "eventId %d.\n", pSeekHookEvent->eventID);
				for(i = 0; i < event->eventCount; i++){
					if(pSeekHookEvent->eventID == event->eventId[i]){
						isFound = 1;
						pTmpHookEvent = pSeekHookEvent;
						pSeekHookEvent = pSeekHookEvent->next;

						DELETE_NODE(pSeekModule->module->eventListOfModule,pTmpHookEvent);

						if(isFound)
							break;
					}
				}
				if(isFound == 0)
					pSeekHookEvent = pSeekHookEvent->next;
			}
			break;
		}
		
		pSeekModule = pSeekModule->next;
	}
	//cmsCore_printModuleList();

	/*remove event & modulist from eventLIst*/
	pSeekEvent = eventList;
	while(pSeekEvent != NULL){
		isFound = 0;

		for(i = 0; i < event->eventCount; i++){
			if(pSeekEvent->event->eventID == event->eventId[i]){
				isFound = 1;
				break;
			}
		}
		
		if(isFound){
			pSeekHookModule = pSeekEvent->event->moduleListOfEvent;
			while(pSeekHookModule != NULL){
				if(pSeekHookModule->moduleInfo.moduleMsqId == moduleMsqId){
					pTmpModule = pSeekHookModule;
					DELETE_NODE(pSeekEvent->event->moduleListOfEvent,pTmpModule);
					break;
				}
				pSeekHookModule = pSeekHookModule->next;
			}
			break;
			/*pTmpEvent = pSeekEvent;
			pSeekEvent = pSeekEvent->next;
			safe_free(pTmpEvent->event);
			DELETE_NODE(eventList, pTmpEvent);*/
		}

		if(isFound == 0)
			pSeekEvent = pSeekEvent->next;
	}
	//cmsCore_printEventList();

 return 0;
}

void cmsCore_printEvent(char *moduleName)
{
	cms_moduleList_t *pSeekModule = moduleList;
	cms_hookEventList_t *pModuleEvent = NULL;
	int foundFlag = 0;

	if(moduleName == NULL){
		while(pSeekModule != NULL){
			printf("********************************************%s********************************************\n", pSeekModule->module->moduleInfo.moduleName);
			printf("moduleName: %s, pid: %u, eventQueue: %u,  moduleQueue %u.\n",
				pSeekModule->module->moduleInfo.moduleName, pSeekModule->module->moduleInfo.pid,
				pSeekModule->module->moduleInfo.eventMsqId,pSeekModule->module->moduleInfo.moduleMsqId);
			printf("eventID:");
			pModuleEvent = pSeekModule->module->eventListOfModule;
			while(pModuleEvent){
				printf("  %u", pModuleEvent->eventID);
				pModuleEvent = pModuleEvent->next;
			}
			printf("\n");
			printf("********************************************%s********************************************\n", pSeekModule->module->moduleInfo.moduleName);
			printf("\n\n");
			
			pSeekModule = pSeekModule->next;
		}
	}else{
		while(pSeekModule != NULL){
			if(strcmp(pSeekModule->module->moduleInfo.moduleName, moduleName) == 0){
				printf("********************************************%s********************************************\n", pSeekModule->module->moduleInfo.moduleName);
				printf("moduleName: %s, pid: %u, eventQueue: %u,  moduleQueue %u.\n",
					pSeekModule->module->moduleInfo.moduleName, pSeekModule->module->moduleInfo.pid,
					pSeekModule->module->moduleInfo.eventMsqId,pSeekModule->module->moduleInfo.moduleMsqId);
				printf("eventID:");
				pModuleEvent = pSeekModule->module->eventListOfModule;
				while(pModuleEvent){
					printf("  %u", pModuleEvent->eventID);
					pModuleEvent = pModuleEvent->next;
				}
				printf("\n");
				printf("********************************************%s********************************************\n", pSeekModule->module->moduleInfo.moduleName);
				printf("\n\n");
				foundFlag = 1;
				break;
			}
			pSeekModule = pSeekModule->next;
		}

		if(foundFlag == 0){
			printf("moduleName %s is not found, maybe name is error!!!\n", moduleName);
		}
	}


	cms_eventList_t *pSeekEvent = eventList;
	while(pSeekEvent != NULL){
		printf("event id: %u,  ", pSeekEvent->event->eventID);
		cms_hookModuleList_t *moduleList = pSeekEvent->event->moduleListOfEvent;
		while(moduleList){
			printf("<%s, %u, %u, %u> ", moduleList->moduleInfo.moduleName, moduleList->moduleInfo.pid, 
				moduleList->moduleInfo.moduleMsqId, moduleList->moduleInfo.eventMsqId);

			moduleList = moduleList->next;
		}
		printf("\n");
		
		if(pSeekEvent->next == NULL){
			pSeekEvent = NULL;
			break;
		}
		pSeekEvent = pSeekEvent->next;
	}
}


